//
// Created by Abstergo on 24-6-22.
//

#ifndef WRAPPER_HPP
#define WRAPPER_HPP

#include <base/defs.h>
#include <functional>

namespace tbs {
/**
* 包装器基类
* @tparam T 包装类型
*/
template<typename T> class Wrapper {
public:
  /**
   * 取包装的值
   * @return
   */
  T &operator*();

  /**
   * 取包装的值
   * @return
   */

  virtual T &value() = 0;

  Wrapper<T> &operator=(CONST T &val) {
	value() = val;
	return *this;
  }

  /**
   * 静态释放包装
   * @param item
   */
  static void releaseWrap(Wrapper<T> &item);

  virtual ~Wrapper();

protected:
  virtual void m_release() = 0;

private:
  bool _hasRelease = false;
};

template<typename V> inline static void WrappedDestruction(Wrapper<V> *v);

template<typename V> class FunctionWrapper : public Wrapper<V> {
public:
  using release_func = std::function<void(FunctionWrapper<V> &)>;
  explicit FunctionWrapper(V &va, release_func rel)
	  : _val(va), _r(rel) {
  }

  V &value() override {
	return _val;
  }

protected:
  void m_release() override {
	if (_r == nullptr) {
	  return;
	}
	_r(*this);
  }

private:
  V &_val;
  release_func _r;
};

#include "inls/base/Wrapper.cuh"
}

#endif //WRAPPER_HPP
